//    Copyright (C) Clear Office.  All rights reserved.
using System.Diagnostics;
using System.IO;
using System.IO.Packaging;
using System.Windows.Controls;


namespace Office.Spreadsheet.UI
{
    class XBapClipboard: ClipboardBase
    {
        internal override void SetRange(Range selection)
        {
            SpreadsheetDocument doc = RangeOperations.RangeToWorkbook(selection);
            memoryStream = new MemoryStream();
            Package package = Package.Open(memoryStream, FileMode.Create, FileAccess.ReadWrite);
            doc.WritePackage(package);
            package.Close();
           //\\ RichTextBox rtb = new RichTextBox();
           //\\ rtb.Document = selection.ToFlowDocument("\t");
           //\\ rtb.SelectAll();
           //\\ rtb.Copy();
        }

        private MemoryStream memoryStream;
        internal override Range Copied()
        {
            if (memoryStream == null)
                return null;
            memoryStream.Position = 0;
            Stream stream = memoryStream;
            
                Debug.WriteLine("Clear office found");
                Package p = Package.Open(stream);
                SpreadsheetDocument doc = new SpreadsheetDocument(SpreadsheetContext.Default);
                doc.ReadPackage(p);
                p.Close();
                Worksheet ws = doc.Workbook.View.ActiveSheet as Worksheet;
                if (ws != null)
                    return ws.Selection;
                return null;
            
        }

        internal override void SetSelectionAddress(string address)
        {
            throw new System.NotImplementedException();
        }

        internal override bool GetHasSelectionAddress()
        {
            throw new System.NotImplementedException();
        }
    }
}
